<?php 
// No direct access
defined('_JEXEC') or die;
jimport( 'joomla.user.authentication' );

// Include the JLog class.
jimport('joomla.log.log');

/**
 * Provides the data from Schluh Eventlist for AnJoLinA
 * updated to 2.5.4
 * 
 * @since		1.7
 */
class AJLEventlist
{
	/*
	public static function someFunction($method_name, $params, $app_data)
	{
		return "called some function";
	}
	*/
	
	
	/**
	 * provides all essential Eventlist data (without actual authentication)
	 */
	public static function getEventlistData($method_name, $params, $app_data)
	{
	
		$AJLparams = &JComponentHelper::getParams('com_anjolina'); 
		$debug = $AJLparams->get('debugmode');
		
		if($debug=="1"||$debug=="2"){
			// Add the logger.
			JLog::addLogger(
				array(
		            // Set the name of the log file
		            'text_file' => 'com_anjolina.log.php',
		            // (optional) you can change the directory
		            'text_file_path' => 'logs/anjolina'
			     )
			);
			JLog::add("AJL Eventlist: ".$method_name."<br>", JLog::DEBUG);
		}
		
		$userdata = AnJoLinABasics::validateSession($method_name, $params, $app_data);
		
		$granted_access = $userdata['userviewlevel']; 

		if($debug=="2")
			JLog::add("granted access (ajleventlist):<br>".$granted_access."<br>", JLog::DEBUG);
		
		$eventdata[] = array();
		if($granted_access>1){
			$eventdata = array(AJLEventlist::getEvents($method_name, $params, $app_data),
							AJLEventlist::getEventCategories($method_name, $params, $app_data),
							AJLEventlist::getEventVenues($method_name, $params, $app_data));
		}
					
		return $eventdata;
	}
	
	
	public static function getEvents($method_name, $params, $app_data)
	{
		$AJLparams = &JComponentHelper::getParams('com_anjolina'); 
		$debug = $AJLparams->get('debugmode');
		
		if($debug=="1"||$debug=="2"){
			// Add the logger.
			JLog::addLogger(
				array(
		            // Set the name of the log file
		            'text_file' => 'com_anjolina.log.php',
		            // (optional) you can change the directory
		            'text_file_path' => 'logs/anjolina'
			     )
			);
			JLog::add("AJL Eventlist: ".$method_name."<br>", JLog::DEBUG);
		}
		
		$data = array();
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);

		$sqlstring = "SELECT * FROM `#__eventlist_events`";
		
		$db->setQuery($sqlstring);
		
		if($db->query()){
			$rows = $db->loadAssocList();
			foreach($rows as $key => $value){
				$data[] = array("id" => $value['id'],
								"locid" => $value['locid'],
								"catsid" => $value['catsid'],
								"dates" => $value['dates'],
								"enddates" => $value['enddates'],
								"times" => $value['times'],
								"endtimes" => $value['endtimes'],
								"title" => $value['title'],
								"createdby" => $value['created_by'],
								"modified" => $value['modified'],
								"modifiedby" => $value['modified_by'],
								"created" => $value['created'],
								"description" => $value['datdescription'],
								"recurrencenumber" => $value['recurrence_number'],
								"recurrencetype" => $value['recurrence_type'],
								"recurrencecounter" => $value['recurrence_counter'],
								"image" => $value['datimage']
								);
			}
			
			
			
		}
		else{
			//Error
		}	
		
		return $data;
	}
	
	public static function getEventCategories($method_name, $params, $app_data)
	{
		$AJLparams = &JComponentHelper::getParams('com_anjolina'); 
		$debug = $AJLparams->get('debugmode');
		
		if($debug=="1"||$debug=="2"){
			// Add the logger.
			JLog::addLogger(
				array(
		            // Set the name of the log file
		            'text_file' => 'com_anjolina.log.php',
		            // (optional) you can change the directory
		            'text_file_path' => 'logs/anjolina'
			     )
			);
			JLog::add("AJL Eventlist: ".$method_name."<br>", JLog::DEBUG);
		}
		
		$data = array();
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);

		$sqlstring = "SELECT * FROM `#__eventlist_categories`";
		
		$db->setQuery($sqlstring);
		
		if($db->query()){
			$rows = $db->loadAssocList();

			foreach($rows as $key => $value){

				$data[] = array("id" => $value['id'],
								"parent" => $value['parent_id'],
								"catname" => $value['catname'],
								"alias" => $value['alias'],
								"description" => $value['catdescription'],
								"ordering" => $value['ordering'],
								"image" => $value['image']
								);
			}
			
			
			
		}
		else{
			//Error
		}	
		
				
		return $data;
	}
	public static function getEventVenues($method_name, $params, $app_data)
	{
		$AJLparams = &JComponentHelper::getParams('com_anjolina'); 
		$debug = $AJLparams->get('debugmode');
		
		if($debug=="1"||$debug=="2"){
			// Add the logger.
			JLog::addLogger(
				array(
		            // Set the name of the log file
		            'text_file' => 'com_anjolina.log.php',
		            // (optional) you can change the directory
		            'text_file_path' => 'logs/anjolina'
			     )
			);
			JLog::add("AJL Eventlist: ".$method_name."<br>", JLog::DEBUG);
		}
		
		$data = array();

		$db = JFactory::getDbo();
		$query = $db->getQuery(true);

		$sqlstring = "SELECT * FROM `#__eventlist_venues`";
		
		$db->setQuery($sqlstring);
		
		if($db->query()){
			$rows = $db->loadAssocList();
			
			foreach($rows as $key => $value){
				
				$data[] = array("id" => $value['id'],
								"venue" => $value['venue'],
								"alias" => $value['alias'],
								"url" => $value['url'],
								"street" => $value['street'],
								"plz" => $value['plz'],
								"city" => $value['city'],
								"state" => $value['state'],
								"country" => $value['country'],
								"description" => $value['locdescription'],
								"created" => $value['created'],
								"createdby" => $value['created_by'],
								"modified" => $value['modified'],
								"modifiedby" => $value['modified_by'],
								"ordering" => $value['ordering'],
								"image" => $value['locimage']
								);
			}	
			
		}
		else{
			//Error
		}	
		
		return $data;
	}	
	
}
?>